<!DOCTYPE html>
<meta charset="utf-8">
<html>
    <head>
        <style>
            rect.bordered {
                stroke: #E6E6E6;
                stroke-width:2px;   
            }

            text.mono {
                font-size: 9pt;
                font-family: Consolas, courier;
                fill: #aaa;
            }

            text.axis-workweek {
                fill: #000;
            }

            text.axis-worktime {
                fill: #000;
            }
        </style>
        <script src="http://d3js.org/d3.v3.min.js"></script>
        <script src="https://raw.github.com/mbostock/d3/master/lib/colorbrewer/colorbrewer.js"></script>
    </head>
    <body>
        <aside style="margin-top:20px;">
            <p>Order: <select id="order">
                    <option value="name">by Name</option>
                    <option value="name">by value</option>
                </select>

        </aside>
        <div id="chart"></div>

        <script type="text/javascript">
            var margin = { top: 50, right: 0, bottom: 100, left: 30 },
            width = 960 - margin.left - margin.right,
            height = 930 - margin.top - margin.bottom,
            gridSize = Math.floor(width / 24),
            buckets = 9,
            colors = colorbrewer.YlGnBu[9],
            days = ["Mon", "Tu", "We", "Th", "Fr", "Sa", "Su"],
            times = ["1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12a", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p", "12p"];
            var x1 = d3.scale.ordinal().rangeBands([0, width]),
            y1 = d3.scale.ordinal().rangeBands([0, height]);
 
            d3.tsv("data.tsv",
            function(d) {
                return {
                    day: +d.day,
                    hour: +d.hour,
                    value: +d.value
                };
            },
            function(error, data) {
                var colorScale = d3.scale.quantile()
                .domain([0, buckets - 1, d3.max(data, function (d) { return d.value; })])
                .range(colors);
                
                var svg = d3.select("#chart").append("svg")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
                .append("g")
                .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
 
                var dayLabels = svg.selectAll(".dayLabel")
                .data(days)
                .enter().append("text")
                .text(function (d) { return d; })
                .attr("x", 0)
                .attr("y", function (d, i) { return i * gridSize; })
                .style("text-anchor", "end")
                .attr("transform", "translate(-6," + gridSize / 1.5 + ")")
                .attr("class", function (d, i) { return ((i >= 0 && i <= 4) ? "dayLabel mono axis axis-workweek" : "dayLabel mono axis"); });
 
                var timeLabels = svg.selectAll(".timeLabel")
                .data(times)
                .enter().append("text")
                .text(function(d) { return d; })
                .attr("x", function(d, i) { return i * gridSize; })
                .attr("y", 0)
                .style("text-anchor", "middle")
                .attr("transform", "translate(" + gridSize / 2 + ", -6)")
                .attr("class", function(d, i) { return ((i >= 7 && i <= 16) ? "timeLabel mono axis axis-worktime" : "timeLabel mono axis"); });
                console.log(data);
       
                var heatMap = svg.selectAll(".hour")
                .data(data)
                .enter().append("rect")
                .attr("x", function(d) { return (d.hour - 1) * gridSize; })
                .attr("y", function(d) { return (d.day - 1) * gridSize; })
                .attr("rx", 4)
                .attr("ry", 4)
                .attr("class", "hour bordered")
                .attr("width", gridSize)
                .attr("height", gridSize)
                .style("fill", colors[0]);
                console.log(data);
                var orders = {
                    name: d3.range(days.length).sort(function(a, b) { return d3.ascending(days[a], days[b]); })  
                };
                y1.domain(orders.name);
                x1.domain(days);
                // for order selection combobox
                d3.select("#order").on("change", function() {
                    order(this.value);
                });
                function order(value) {
                    // cluster option is selected, show the dendogram
                    // the other option is selected, hide the dendogram
                    y1.domain(orders[value]);
                    console.log(value);
                    var t = svg.transition().duration(2500);

                    //when count is selected, the color of each cell is zscore
                    t.selectAll(".hour")
                    .delay(function(d, i) { return x1(i); })
                    .attr("transform", function(d, i) { return "translate(0," + x1(i) + ")"; })
                    .attr("x", function(d, i) { return y1(i); })
                    .style("fill", function(d) { return d.value;});
                }
 
                heatMap.transition().duration(1000)
                 .style("fill", function(d) { return colorScale(d.value); });
 
                heatMap.append("title").text(function(d) { return d.value; });
            });
        </script>
    </body>
</html>